<?php

include_once APPLICATION_DIR . "/application/model/CATEGORY_VIDEO_PROVIDER.php";

class Category_Video_Provider extends DataObjects_CATEGORY_VIDEO_PROVIDER 
{
    function deleteCategoryVideoProvider($id_provider_category) {
        $category_video_provider = new Category_Video_Provider();
        $category_video_provider->ID_PROVIDER_CATEGORY = $id_provider_category;
        $category_video_provider->delete();
    }
    
    function insertCategoryVideoProvider($id_provider_category, $id_categories = array()) {
        $category_video_provider = new Category_Video_Provider();
        $sql = "";
        foreach ($id_categories as $id_category) {
            if ($sql) {
                $sql .= ", ({$id_provider_category}, {$id_category})";
            } else {
                $sql = "({$id_provider_category}, {$id_category})";
            }
        }
        $sql = "INSERT INTO CATEGORY_VIDEO_PROVIDER (ID_PROVIDER_CATEGORY, ID_CATEGORY) VALUES".$sql;
        $category_video_provider->query($sql);
    }

    function getI18nRealValueSql($field_name, $lang_input = '')
	{
        if (!$lang_input) {
            $lang_input = DRX_Multilanguage::getLanguage();
        }
        if (strlen($lang_input) == 2) {
            $lang_input = strtolower($lang_input);
        }
        $len_lang_input = strlen($lang_input)+2;

        $lang_default = DEFAULT_LANGUAGE;
        $len_lang_default = strlen($lang_default)+2;

        $str = "SUBSTR(".$field_name.",
		IF(POSITION('<".$lang_input.">' IN ".$field_name.")=0,
			( IF(POSITION('<".$lang_default.">' IN ".$field_name.")=0, POSITION('>' IN ".$field_name.")+1, POSITION('<".$lang_default.">' IN ".$field_name.")+".$len_lang_default.") ),
		POSITION('<".$lang_input.">' IN ".$field_name.")+".$len_lang_input."))";
        $str = "TRIM(SUBSTR($str, 1, POSITION('</' IN $str)-1))";
		return $str;
	}
	
   function getIdCategories($id_provider = 0, $categories = '', $lang_code='')
   {
        $arr_cat = explode(",", $categories);
        $id_categories = "";
        foreach($arr_cat as $cat){
            if($cat!="")
            {
                $category_video_provider = new Category_Video_Provider();
                $query = "        SELECT DISTINCT        CATEGORY_VIDEO_PROVIDER.ID_CATEGORY
                            FROM PROVIDER_CATEGORY
                            INNER JOIN CATEGORY_VIDEO_PROVIDER ON CATEGORY_VIDEO_PROVIDER.ID_PROVIDER_CATEGORY = PROVIDER_CATEGORY.ID
                            WHERE ID_PROVIDER = ".$id_provider." AND ".$this->getI18nRealValueSql("NAME",$lang_code)." IN ('$cat')" ;
                $category_video_provider->query($query);
                $is_existed = false;
                while ($category_video_provider->fetch())
                {
                    $is_existed = true;
                    if (!$id_categories) {
                        $id_categories = $category_video_provider->ID_CATEGORY;
                    } else {
                        $id_categories .=",".$category_video_provider->ID_CATEGORY;
                    }
                }
                if(!$is_existed)
                {
                    $provider_category = new Provider_Category();
                    $query = "INSERT INTO PROVIDER_CATEGORY (ID_PROVIDER,NAME) VALUES ('{$id_provider}','<{$lang_code}>{$cat}</{$lang_code}>')
                    ON DUPLICATE KEY UPDATE `ID_PROVIDER` = VALUES(`ID_PROVIDER`),`NAME` = VALUES(`NAME`)";
                    $result = $provider_category->query($query);
                }
            }
        }
        return $id_categories;
    }
    function getIdCategoriesName($id_provider = 0, $id_categories = '')
    {
        $category_video_provider = new Category_Video_Provider();
        $query = "        SELECT DISTINCT PROVIDER_CATEGORY.NAME
                    FROM PROVIDER_CATEGORY
                    INNER JOIN CATEGORY_VIDEO_PROVIDER ON CATEGORY_VIDEO_PROVIDER.ID_PROVIDER_CATEGORY = PROVIDER_CATEGORY.ID
                    WHERE ID_PROVIDER = ".$id_provider." AND  ID_CATEGORY IN ($id_categories)" ;
        $category_video_provider->query($query);
        $categories_name = "";
        $is_existed = false;
        while ($category_video_provider->fetch())
        {
            $is_existed = true;
            if (!$categories_name) {
                $categories_name = DRX_StringUtils::getI18n($category_video_provider->NAME);
            } else {
                $categories_name .=",".DRX_StringUtils::getI18n($category_video_provider->NAME);
            }
        }
        return $categories_name;
    }

}
